﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EI.SI;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Diagnostics;

namespace Server
{
    class Program
    {

        static void Main(string[] args)
        {
            int port = 9999;
            Console.Write("A espera de cliente...");
            TcpListener tcpListener = new TcpListener(IPAddress.Any, port);
            tcpListener.Start();

            //waits for a client connection
            TcpClient tcpClient = tcpListener.AcceptTcpClient();
            Console.WriteLine("ok");

            NetworkStream stream = tcpClient.GetStream();
            FileStream fsWrite = null;

            int n;
            ProtocolSI protocol = new ProtocolSI();
            byte[] msg;
            ProtocolSICmdType cmdType = ProtocolSICmdType.ACK;

            do{
            //read ?? from client s
            n = stream.Read(protocol.Buffer, 0, protocol.Buffer.Length);
            cmdType = protocol.GetCmdType();
            //Debug.WriteLine("qual o cmdType: "+cmdType); //debug do cmdType
            //if (cmdType == ProtocolSICmdType.NORMAL)
            //Console.WriteLine("number = " + protocol.GetIntFromData());

            if (cmdType == ProtocolSICmdType.DATA)
            {
                //Console.WriteLine("string = " + protocol.GetStringFromData());

                FileStream fsWrite2 = new FileStream("icon2.gif", FileMode.Create, FileAccess.Write);
                fsWrite2.Write(protocol.GetData(), 0, protocol.GetDataLength());
                fsWrite2.Close();

            }
            else if (cmdType == (ProtocolSICmdType)20) //nome do ficheiro CORRIGIR ESTE CODIGO
            {
                //Console.WriteLine("Nome do ficheiro:"+ protocol.GetStringFromData());
                fsWrite = new FileStream(protocol.GetStringFromData(), FileMode.Create, FileAccess.Write);

                //enviar ACK
                msg = protocol.Make(ProtocolSICmdType.ACK);
                stream.Write(msg, 0, msg.Length);
            }
            else if (cmdType == (ProtocolSICmdType)21) //MULTI PEDACOS CORRIGIR ESTE CODIGO
            {

                fsWrite.Write(protocol.GetData(), 0, protocol.GetDataLength());
                //enviar ACK
                msg = protocol.Make(ProtocolSICmdType.ACK);
                stream.Write(msg, 0, msg.Length);
            }
            else if (cmdType == ProtocolSICmdType.EOF) //FINAL DO FICHEIRO
            {
                fsWrite.Close();
            }

            //send ack
            msg = protocol.Make(ProtocolSICmdType.ACK);
            stream.Write(msg, 0, msg.Length);

            } while (cmdType != ProtocolSICmdType.EOT);

            //release resources
            stream.Close();
            tcpClient.Close();
            tcpListener.Stop();

            Console.WriteLine("fim ");
            Console.ReadKey();
        }
    }
}
